﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Win32;
using System.Reflection;

namespace SPNetworkTopology.ExternalWindows.ViewModel
{
    class FrmAboutViewModel:INotifyPropertyChanged
    {
        RelayCommand okCommand;
        RelayCommand navigateCommand;

        public string LabelContent
        {
            get
            {
                return String.Concat(SPNetworkTopologyResource.AddInName + " version - " +
                    Assembly.GetExecutingAssembly().GetName().Version.ToString());
            }
        }

        #region OkCommand

        /// <summary>
        /// Returns the command which, when executed, cancels the order.      
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (okCommand == null)
                    okCommand = new RelayCommand(() => this.OkOrder());

                return okCommand;
            }
        }

        void OkOrder()
        {            
            this.OnCancelRequestClose();
        }

        #endregion 

        #region NavigateCommand

        /// <summary>
        /// Returns the command which, when executed, opens the url in the default browser.      
        /// </summary>
        public ICommand NavigateCommand
        {
            get
            {
                if (navigateCommand == null)
                    navigateCommand = new RelayCommand(() => this.NavigateURL());

                return navigateCommand;
            }
        }

        void NavigateURL()
        {
            try
            {
                string browserPath = GetDefaultBrowserPath();
                System.Diagnostics.Process.Start(browserPath, SPNetworkTopologyResource.DownLoadUrl);
            }
            catch (System.Exception ex)
            {
                
            }           
        }

        private static string GetDefaultBrowserPath()
        {
            string path = "";
            try
            {
                string key = "htmlfile\\shell\\open\\command";
                RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(key, false);
                path = ((string)registryKey.GetValue(null, null)).Split('"')[1];
            }
            catch (System.Exception ex)
            {
                
            }
            return path;
        }
        #endregion 
        
        /// <summary>
        /// Raise this event on button ok click
        /// </summary>
        private void OnCancelRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raised when the wizard should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
